﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO_MechSys;
using BLL_MechSys;

namespace FORMS_MechSys
{
    public partial class frmMessageBox : Form
    {
        DTOCor dtoCorFundo = new DTOCor();
        DTOCor dtoCorPrincipal = new DTOCor();
        DTOCor dtoCorLetra = new DTOCor();
        BLLCor bllCor = new BLLCor();
        string Texto, Titulo;

        public DialogResult Result { get; set; }
        public MessageBoxIcon MBIcons { get; set; }
        public MessageBoxButtons MBButtons { get; set; }

        public frmMessageBox()
        { 
            InitializeComponent();
        }

        public DialogResult Show(string text, string caption, MessageBoxButtons mbButtons, MessageBoxIcon mbIcons)
        {
            SetColors();
            MBButtons = mbButtons;
            MBIcons = mbIcons;
            Texto = text;
            Titulo = caption;
            LoadTextos();
            LoadButtons();
            LoadIcons();
            this.ShowDialog();
            return Result;
        }

        private void SetColors()
        {
            dtoCorFundo = bllCor.Buscar("Fundo");
            dtoCorPrincipal = bllCor.Buscar("Principal");
            dtoCorLetra = bllCor.Buscar("Letra");

            pictureBox21.BackColor = pictureBox22.BackColor = pictureBox23.BackColor = pictureBox24.BackColor =
                pictureBox25.BackColor = pictureBox26.BackColor = pictureBox27.BackColor = pictureBox28.BackColor =
                pictureBox30.BackColor = pictureBox31.BackColor = tlpTitulo.BackColor =
                tlpMensagem.BackColor = label2.BackColor =
                btn1.BackColor = btn2.BackColor = btn3.BackColor = tlpInterno.BackColor = dtoCorPrincipal.Cor;
            label2.ForeColor = btn3.ForeColor = btn2.ForeColor = btn1.ForeColor = label1.ForeColor = dtoCorLetra.Cor;
            tlpPrincipal.BackColor = tableLayoutPanel2.BackColor = dtoCorFundo.Cor;
        }

        public DialogResult Show(string text, string caption, MessageBoxButtons mbButtons)
        {
            MBButtons = mbButtons;
            SetColors();
            Texto = text;
            Titulo = caption;
            LoadTextos();
            LoadButtons();
            LoadIcons();
            this.ShowDialog();
            return Result;
        }

        public DialogResult Show(string text, string caption)
        {
            SetColors();
            Texto = text;
            Titulo = caption;
            LoadTextos();
            LoadButtons();
            LoadIcons();
            this.ShowDialog();
            return Result;
        }

        public DialogResult Show(string text)
        {
            SetColors();
            Texto = text;
            LoadTextos();
            LoadButtons();
            LoadIcons();
            this.ShowDialog();
            return Result;
        }

        private void LoadButtons()
        {
            if (MBButtons == MessageBoxButtons.OK)
            {
                btn1.Text = "OK";
                btn1.Visible = true;
            }
            if (MBButtons == MessageBoxButtons.AbortRetryIgnore)
            {
                btn1.Text = "Abortar";
                btn2.Text = "Tentar Novamente";
                btn3.Text = "Ignorar";
                btn1.Visible = true;
                btn2.Visible = true;
                btn3.Visible = true;
                btn3.Focus();
            }
            if (MBButtons == MessageBoxButtons.OKCancel)
            {
                btn1.Text = "OK";
                btn2.Text = "Cancelar";
                btn1.Visible = true;
                btn2.Visible = true;
                btn1.Focus();
            }
            if (MBButtons == MessageBoxButtons.RetryCancel)
            {
                btn1.Text = "Cancelar";
                btn2.Text = "Tentar Novamente";
                btn1.Focus();
                btn1.Visible = true;
                btn2.Visible = true;
            }
            if (MBButtons == MessageBoxButtons.YesNo)
            {
                btn2.Text = "Sim";
                btn1.Text = "Não";
                btn2.Focus();
                btn1.Visible = true;
                btn2.Visible = true;
            }
            if (MBButtons == MessageBoxButtons.YesNoCancel)
            {
                btn1.Text = "Cancelar";
                btn2.Text = "Não";
                btn3.Text = "Sim";
                btn3.Focus();
                btn1.Visible = true;
                btn3.Visible = true;
                btn2.Visible = true;
            }
        }

        private void LoadIcons()
        {
            imgIcon.Image = null;
            bool black = false;
            DTOCor dtoCorFundo = new BLL_MechSys.BLLCor().Buscar("Fundo");
            if ((dtoCorFundo.R + dtoCorFundo.G + dtoCorFundo.B) > 360 && dtoCorFundo.R != Color.Black.R && dtoCorFundo.G != Color.Black.G && dtoCorFundo.B != Color.Black.B)
                black = true;
            if (dtoCorLetra.R == Color.White.R && dtoCorLetra.G == Color.White.G && dtoCorLetra.B == Color.White.B)
                black = false;
            if (MBIcons != MessageBoxIcon.None)
            {
                if (MBIcons == MessageBoxIcon.Error)
                    if (black)
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Error1;
                    else
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Error;
                else if (MBIcons == MessageBoxIcon.Information)
                    if (black)
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Info1;
                    else
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Info;
                else if (MBIcons == MessageBoxIcon.Warning)
                    if (black)
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Problem1;
                    else
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Problem;
                else
                    if (black)
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Problem1;
                    else
                        imgIcon.Image = global::FORMS_MechSys.Properties.Resources.Problem;
            }
        }

        private void LoadTextos()
        {
            this.Text = label2.Text = Titulo;
            label1.Text = Texto;
            int margin;
            margin = ((int)(tlpTitulo.Width - label2.Width) / 2) - tableLayoutPanel3.Width;
            if (margin < 0)
                margin = 0;
            label2.Margin = new Padding(margin, 5, 0, 0);
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            Resultado(btn1);
            this.Close();
            
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            Resultado(btn2);
            this.Close();
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            Resultado(btn3);
            this.Close();
        }


        private void Resultado(Control controle)
        {
            if (controle.Text == "OK")
                Result = System.Windows.Forms.DialogResult.OK;
            else if (controle.Text == "Sim")
                Result = System.Windows.Forms.DialogResult.Yes;
            else if (controle.Text == "Não")
                Result = System.Windows.Forms.DialogResult.No;
            else if (controle.Text == "Cancelar")
                Result = System.Windows.Forms.DialogResult.Cancel;
            else if (controle.Text == "Tentar Novamente")
                Result = System.Windows.Forms.DialogResult.Retry;
            else if (controle.Text == "Abortar")
                Result = System.Windows.Forms.DialogResult.Abort;
            else if (controle.Text == "Ignorar")
                Result = System.Windows.Forms.DialogResult.Ignore;
        }


    }
}
